<!-- 人脉配置 -->
<template>
  <div class="warp">
    <div class="content">
      <div class="title">资源页面控制</div>
      <div class="info">
        <div class="desc">关闭后，人脉资源、单位资源对外不可见</div>
        <a-checkbox-group v-model="checkeds" @change="handleChangeCheckbox">
          <a-row class="flex-line">
            <a-col :span="24">
              <a-checkbox value="connectionResourceDisplay"> 展示会员人脉资源页 </a-checkbox>
            </a-col>
          </a-row>
          <a-row class="flex-line">
            <a-col :span="24">
              <a-checkbox value="unitResourceDisplay"> 展示会员单位资源页 </a-checkbox>
            </a-col>
          </a-row>
        </a-checkbox-group>
        <!-- <div class="flex-line">
          <span>展示会员人脉资源页</span>
          <a-switch v-model="resData.connectionResourceDisplay" @change="handleChangeConfig" />
        </div>
        <div class="flex-line">
          <span>展示会员单位资源页</span>
          <a-switch v-model="resData.unitResourceDisplay" @change="handleChangeConfig" />
        </div> -->
      </div>
    </div>
    <div class="content">
      <div class="title">秘书处联络人脉审核配置</div>
      <div class="info">
        <div class="desc">关闭后，会内查看会外人脉无需本会管理员审核</div>
        <div class="flex-line">
          <span>会内人员查看合作、联盟组织人脉是否需要审核</span>
          <a-switch v-model="resData.isInternalSharing" @change="handleChangeConfig" />
        </div>
      </div>
    </div>

    <div v-if="false" class="content">
      <div class="title">资源初始配置</div>
      <div class="info">
        <div class="desc">设置商协会合作、联盟合作对外展示的初始单价。</div>
        <s-table ref="table" :showPagination="false" :columns="columns" :data="loadData">
          <template slot="price" slot-scope="text, record, index">
            <div class="table-item">
              <span>解锁联系方式（￥）</span>
              <EditableCell :text="record.price1" @change="onCellChange(record, $event, index, 'price1')" />
            </div>
            <div class="table-item">
              <span>秘书处联络（￥）</span>
              <EditableCell :text="record.price2" @change="onCellChange(record, $event, index, 'price2')" />
            </div>
          </template>
        </s-table>
      </div>
    </div>

    <div class="content">
      <div class="title">配置审核人员与联络人员</div>
      <div class="info">
        <div class="desc">
          审核用户发起的秘书处撮合业务，例如通过秘书处撮合人脉业务，谁审核建立群聊时系统会自动拉谁进入群聊。
        </div>
        <div class="approval-user">
          <span>指定审核人</span>
          <a-select
            class="select-user"
            v-model="userIds"
            mode="multiple"
            placeholder="请选择"
            @select="handleSelect"
            @deselect="handleDeselect"
          >
            <a-select-option v-for="item in user_list" :key="item.userId">{{ item.userName }}</a-select-option>
          </a-select>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ConfigInfo, ConfigUpdate, ManagerList, ManagerAdd, ManagerDel, TenantInfo, TenantSave } from '@/api/business/source'
import { getUserList } from '@/api/system/system'
import { STable } from '@/components'
import EditableCell from '@/components/EditableCell'
export default {
  components: {
    STable,
    EditableCell,
  },
  data() {
    return {
      checkeds: [],
      resData: {
        connectionResourceDisplay: false,
        unitResourceDisplay: false,
        isInternalSharing: false,
      },
      user_list: [],
      manager_list: [],
      userIds: [],
      source_list: [],
      // columns: [
      //   {
      //     title: '资源',
      //     dataIndex: 'projectName',
      //   },
      //   {
      //     title: '价格设置（元）',
      //     scopedSlots: { customRender: 'price' },
      //   },
      // ],
      // loadData: () => {
      //   return TenantInfo().then((res) => {
      //     this.source_list = res.data || []
      //     return res
      //   })
      // },
    }
  },
  computed: {},
  watch: {},
  methods: {
    async getConfigInfo() {
      let res = await ConfigInfo()
      this.resData = res.data
      this.checkeds = []
      Object.keys(this.resData).forEach((k) => {
        if (this.resData[k] === 0) this.checkeds.push(k)
      })
      this.resData = this.convertNumberToBoolean({ ...this.resData })
    },
    async GetUserList() {
      let res = await getUserList()
      this.user_list = res.rows || []
    },
    async getManagerList() {
      let res = await ManagerList()
      this.manager_list = res.data || []
      this.userIds = (res.data || []).map((item) => item.systemId)
    },
    convertNumberToBoolean(data) {
      Object.keys(data).forEach((k) => {
        data[k] = data[k] === 0 ? true : false
      })
      return data
    },
    convertBooleanToNumber(data) {
      Object.keys(data).forEach((k) => {
        data[k] = data[k] === true ? 0 : 1
      })
      return data
    },
    handleChangeCheckbox(val) {
      if (!val.length) {
        this.$message.error('至少保留一项')
        this.getConfigInfo()
      } else {
        Object.keys(this.resData).forEach((k) => {
          if (k != 'isInternalSharing') {
            this.resData[k] = val.includes(k)
          }
        })
        this.handleChangeConfig()
      }
    },
    async handleChangeConfig() {
      let data = this.convertBooleanToNumber({ ...this.resData })
      let { code, msg } = await ConfigUpdate({ ...data })
      this.$message[code === 0 ? 'success' : 'error'](msg)
      this.getConfigInfo()
    },
    async handleSelect(val) {
      let item = this.user_list.find((f) => f.userId === val) || {}
      let { code, msg } = await ManagerAdd({ systemId: val, systemName: item.userName, systemPhone: item.phonenumber })
      this.$message[code === 0 ? 'success' : 'error'](msg)
      if (code != 0) this.userIds.pop()
      this.getManagerList()
    },
    async handleDeselect(val) {
      let item = this.manager_list.find((f) => f.systemId === val) || {}
      let { code, msg } = await ManagerDel(item.id)
      this.$message[code === 0 ? 'success' : 'error'](msg)
      this.getManagerList()
    },
    // async onCellChange(item, val, index, field) {
    //   item[field] = val
    //   this.source_list[index] = item
    //   let { code, msg } = await TenantSave([...this.source_list])
    //   this.$message[code === 0 ? 'success' : 'error'](msg)
    //   this.$refs.table.refresh()
    // },
  },
  created() {
    this.getConfigInfo()
    this.GetUserList()
    this.getManagerList()
  },
  mounted() {},
}
</script>
<style lang="less" scoped>
.warp {
  padding: 20px;
  background: #fff;
  border-radius: 20px;
  .content {
    width: 50%;
    margin-bottom: 50px;
    .title {
      font-size: 18px;
      font-weight: bold;
    }
    .info {
      margin: 20px;
      .desc {
        color: #999;
      }
      .flex-line {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 15px;
        font-size: 16px;
        font-weight: bold;
      }
      .approval-user {
        display: flex;
        align-items: center;
        margin-top: 15px;
        .select-user {
          margin-left: 10px;
          width: 300px;
        }
      }
    }
  }
}

.table-item {
  display: flex;
  align-items: center;
}
</style>